package com.ledim.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.FilterListBean;
import com.ledim.widget.horizontallist.widget.AdapterView;
import com.ledim.widget.horizontallist.widget.HListView;
import com.ledim.widget.view.TagFilterCallbackListener;
import com.letv.android.young.client.R;
import java.util.ArrayList;

public class FiltersAdapter extends LedimBaseListAdapter<FilterListBean> {

    private TagFilterCallbackListener mListener;

    public FiltersAdapter(Context context, ArrayList<FilterListBean> list,
                          TagFilterCallbackListener listener) {
        super(context);
        mListener = listener;
        setDatas(list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_label_tabs, null);
            holder.mHListView =
                    (HListView) convertView.findViewById(R.id.item_channels_tabs_listview);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final FilterListBean filter = getItem(position);
        if (filter.val.size() > 0) {
            holder.mHListView.setVisibility(View.VISIBLE);
            if (holder.mAdapter == null) {
                holder.mAdapter = new SubFiltersAdapter(context, filter.val);
                holder.mHListView.setAdapter(holder.mAdapter);
            } else {
                holder.mAdapter.setDatas(filter.val);
            }
        } else {
            holder.mHListView.setVisibility(View.GONE);
        }
        holder.mHListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (holder.mAdapter.selectedPosition != position) {
                    holder.mAdapter.setSelectItem(position);
                    if (mListener != null) {
                        mListener.onFilter(filter, filter.val.get(position));
                    }
                }
            }
        });

        return convertView;
    }

    public static class ViewHolder {

        private HListView mHListView;
        private SubFiltersAdapter mAdapter;
    }

}
